package graphics.primitive;

import java.util.ArrayList;
import javax.media.opengl.GL;

import mathematics.geometry.Geometry;
import utils.Point3D;

public class Polyline extends BaseDynamicPrimitive 
{
	public float lineWidth = 1;
	public boolean isLoop = false;
	
	public int beginMode;
	
	
	public Polyline(GL gl, Geometry shape) 
	{
		super(gl, shape);
		this.lineWidth = 1.0f;
		this.geometry = shape;
		if(this.geometry.isLoop())
		{
			this.isLoop = true;
		}
		
		this.init(shape);
		
		this.GenerateVertices(gl);
		this.GenerateIndices(gl);
	}
	
	
	public Polyline(GL gl, Geometry shape, float lineWidth) 
	{
		this(gl, shape);
		this.lineWidth = lineWidth;
		this.geometry = shape;
		if(this.geometry.isLoop())
		{
			this.isLoop = true;
		}
		this.init(shape);
		
		this.GenerateVertices(gl);
		this.GenerateIndices(gl);
	}
	
	protected void init(Geometry shape)
	{	

	}

	@Override
	public void GenerateVertices(GL gl) {
	
		ArrayList<Point3D> vertices = this.geometry;
		if(this.isLoop)
		{
			vertices.add(this.geometry.get(0));
		}
		this.dynamicVbo.GenerateVertexBuffer(gl, this.geometry);
	}

	@Override
	public void GenerateIndices(GL gl) 
	{
		short[] indices = new short[this.dynamicVbo.vertexCount];	
		for(short i = 0; i < this.dynamicVbo.vertexCount; i++ )
		{
			indices[i] = i;
		}
		this.dynamicVbo.GenerateIndexBuffer(gl, indices);
	}
}
